package peng.yang.express.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.List;

import peng.yang.express.Activity.CheckActivity;
import peng.yang.express.Adapter.SwipeListViewBaseAdapter;
import peng.yang.express.R;
import peng.yang.express.Bean.ExpressMember;
import swipelistview.swipelistview.BaseSwipeListViewListener;
import swipelistview.swipelistview.SwipeListView;

/**
 * Created by Administrator on 2016/6/24.
 */
public class Fragment02 extends Fragment {

    private View rootView;
    private SwipeListView listView;
    private SwipeListViewBaseAdapter adapter;
    private List<ExpressMember> members;
    private Button remove;
    private LinearLayout noHistory;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_02, null);

        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        findView();
        initData();
        return rootView;
    }
    private void findView() {
        listView = (SwipeListView) rootView.findViewById(R.id.datalist);
        noHistory = (LinearLayout) rootView.findViewById(R.id.history);
    }

    private void initData(){
        listView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);

        //拿到设备宽度
        int deviceWidth =  getResources().getDisplayMetrics().widthPixels;
        listView.setOffsetLeft(deviceWidth * 3 / 5);
        listView.setOffsetRight(deviceWidth * 3/ 5);
        adapter = new SwipeListViewBaseAdapter(getContext(),noHistory);
        if(adapter.getMembers().size()==0){
            noHistory.setVisibility(View.VISIBLE);
            listView.setVisibility(View.INVISIBLE);
        }else {
            noHistory.setVisibility(View.INVISIBLE);
            listView.setVisibility(View.VISIBLE);
            listView.setAdapter(adapter);
        }
        listView.setSwipeListViewListener(new BaseSwipeListViewListener(){
            @Override
            public void onClickFrontView(int position) {
                super.onClickFrontView(position);
                Intent intent = new Intent(getActivity(), CheckActivity.class);
                intent.putExtra("com",adapter.getSigleName(position));
                intent.putExtra("nu",adapter.getNu(position));
                intent.putExtra("goodName",adapter.getGoodName(position));
                startActivity(intent);
            }

            @Override
            public void onDismiss(int[] reverseSortedPositions) {
                super.onDismiss(reverseSortedPositions);
                for(int i:reverseSortedPositions){
                    members.remove(i);
                }
                adapter.notifyDataSetChanged();

            }
        });
    }
}
